package com.xie.smfs.entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;

import java.util.Objects;
import java.util.HashMap;
import java.util.UUID;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.LivingEntity;

public class VillagerGhostEntity extends GhostEntity {
    // 存储玩家最后攻击村民的时间
    private static final HashMap<UUID, Long> playerLastVillagerAttack = new HashMap<>();
    private static final long ATTACK_MEMORY_DURATION = 10000; // 记住攻击行为10秒

    public static DefaultAttributeContainer.Builder createLivingAttributes() {
        return LivingEntity.createLivingAttributes()
                .add(EntityAttributes.GENERIC_MAX_HEALTH, 20.0)
                .add(EntityAttributes.GENERIC_MOVEMENT_SPEED, 0.25)
                .add(EntityAttributes.GENERIC_ATTACK_DAMAGE, 5.0);
    }

    private static final boolean HAS_GHOST_DOMAIN = true; // 启用鬼域功能
    private static final int GHOST_DOMAIN_LEVEL = 0;
    private static final double GHOST_DOMAIN_RADIUS = 32.0;
    private static final char TERROR_LEVEL = 'C'; // C级

    public VillagerGhostEntity(EntityType<? extends GhostEntity> entityType, World world) {
        super(entityType, world, HAS_GHOST_DOMAIN, GHOST_DOMAIN_LEVEL, GHOST_DOMAIN_RADIUS, TERROR_LEVEL);
        Objects.requireNonNull(this.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH)).setBaseValue(20.0);
        Objects.requireNonNull(this.getAttributeInstance(EntityAttributes.GENERIC_MOVEMENT_SPEED)).setBaseValue(0.25);
        Objects.requireNonNull(this.getAttributeInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE)).setBaseValue(5.0);
        this.attackCooldown = 20;

    }

    @Override
    protected boolean shouldAttackPlayer(PlayerEntity player) {
        // 检查玩家是否最近攻击过村民
        if (!isPlayerInRange(player)) {
            return false;
        }

        UUID playerId = player.getUuid();
        long currentTime = System.currentTimeMillis();




        if (playerLastVillagerAttack.containsKey(playerId)) {

            long lastAttackTime = playerLastVillagerAttack.get(playerId);
            long timeDiff = currentTime - lastAttackTime;

            if (timeDiff < ATTACK_MEMORY_DURATION) {

                return true;
            } else {
                playerLastVillagerAttack.remove(playerId);

            }
        } else {

        }

        // 同时检查玩家是否正在攻击村民
        Entity attackingEntity = player.getAttacking();
        boolean isAttackingVillager = attackingEntity instanceof VillagerEntity;

        if (isAttackingVillager) {

        } else {

        }
        return isAttackingVillager;
    }

    private boolean isPlayerInRange(PlayerEntity player) {
        return this.squaredDistanceTo(player) <= GHOST_DOMAIN_RADIUS * GHOST_DOMAIN_RADIUS; // 鬼域范围内
    }

    @Override
    protected void executeAttack(PlayerEntity player) {
        // 瞬移到玩家面前
        this.teleport(player.getX(), player.getY(), player.getZ());
        // 获取 mob 伤害类型的注册表项
        RegistryEntry<DamageType> mobDamageType = this.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE).getEntry(RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("smfs", "ghost"))).orElseThrow(() -> new IllegalStateException("MOB_ATTACK damage type not found"));
        // 创建伤害源
        DamageSource damageSource = new DamageSource(mobDamageType, this);
        player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 20, 1));

        player.damage(damageSource, 100.0f);
    }

    // 添加一个静态方法，用于在其他地方记录玩家攻击村民的行为
    public static void recordVillagerAttack(PlayerEntity player) {
        UUID playerId = player.getUuid();
        long currentTime = System.currentTimeMillis();
        playerLastVillagerAttack.put(playerId, currentTime);
    }
}